package com.ruoyi.system;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: com.ruoyi.system
 * @author: Sunne
 * @date: 2024-12-04 上午11:19
 * @Description: 代码备注说明
 */
public class Te {
    // 定义一个内部类来表示地理坐标点
    private static class GeoPoint {
        double longitude;
        double latitude;

        GeoPoint(double longitude, double latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }
    }

    // 判断点是否在多边形内（使用射线法）
    public static boolean isPointInPolygon(GeoPoint point, List<GeoPoint> polygon) {
        boolean result = false;
        int numVertices = polygon.size();
        GeoPoint extreme = new GeoPoint(point.longitude + 10.0, point.latitude); // 选择一个远离点的位置作为射线起点

        for (int i = 0, j = numVertices - 1; i < numVertices; j = i++) {
            if ((polygon.get(i).latitude > point.latitude) != (polygon.get(j).latitude > point.latitude) &&
                    (point.longitude < (polygon.get(j).longitude - polygon.get(i).longitude) * (point.latitude - polygon.get(i).latitude) /
                            (polygon.get(j).latitude - polygon.get(i).latitude) + polygon.get(i).longitude)) {
                result = !result;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        // 定义一个多边形路径（包含经度和纬度）
        List<GeoPoint> polygon = new ArrayList<>();
        polygon.add(new GeoPoint(116.397428, 39.90923));
        polygon.add(new GeoPoint(116.396427, 39.90823));
        polygon.add(new GeoPoint(116.398429, 39.90823));
        polygon.add(new GeoPoint(116.398429, 39.90923));

        // 定义一个要检查的点
        GeoPoint pointToCheck = new GeoPoint(116.3975, 40.9085); // 示例坐标

        // 判断点是否在多边形内
        boolean isInside = isPointInPolygon(pointToCheck, polygon);
        System.out.println("点是否在多边形内: " + isInside);
    }
}